<?php

namespace App\Http\Requests;

use App\Enum\Decoration;
use App\Enum\ShopType;
use App\Util\ApiResult;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class PutShopApiRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $this->needsJson();
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'shop_type' => 'bail|required|in:'.ShopType::BOTTOM_SHOP.','.ShopType::OFFICE_BUILDING,
            'shop_area' => 'bail|required|integer|min:1',
            'shop_size' => 'bail|required|numeric',
            'rent' => 'bail|required|numeric',
            'decoration' => 'bail|required|in:'.Decoration::BLANK.','.Decoration::JANE_DECORATION.','.Decoration::FINE_DECORATION,
            'shop_name' => 'required',
            'address' => 'required',
            'user_id' => 'required|integer',
            'user_name' => ['bail', 'required', 'regex:/^[A-Za-z0-9\u4e00-\u9fa5]{1,10}$/'],
            'user_tel' => ['bail', 'required', 'regex:/^1[3|4|5|7|8][0-9]{9}$/']
        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     *
     * @return array
     */
    public function messages()
    {
        return [
            'shop_type.required' => '商铺类型必选',
            'shop_type.in' => '商铺类型不正确',
            'shop_area.required'  => '区域必选',
            'shop_area.integer' => '区域只能是整数',
            'shop_area.min' => '区域必须大于1',
            'shop_size.required' => '面积必填',
            'shop_size.numeric' => '面积只能是数字',
            'rent.required' => '租金必填',
            'rent.numeric' => '租金只可以是数字',
            'decoration.required' => '装修程度必选',
            'decoration.in' => '装修程度不正确',
            'shop_name.required' => '商铺名字必填',
            'address.required' => '详细地址必填',
            'user_id.required' => '缺少用户ID',
            'user_id.integer' => '用户ID必须为整数',
            'user_name.required' => '联系人必填',
            'user_name.regex' => '联系人只能输入长度不超过10位的数字、英文、汉字',
            'user_tel.required' => '联系电话必填',
            'user_tel.regex' => '联系电话格式不正确',
        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function formatErrors(Validator $validator)
    {
        return ApiResult::failure( $validator->errors()->first() )->getResult();
    }
}
